Receive XMODEM Form1 Label1 Receving to file: filename filename Label4 Receive Mode: filemode filemode Label2 Bytes Received: rbytes rbytes Label3 Errors: Recent/Total: CommandAbort Abort errors errors Form_Click Form_Paint serialbinary NextPacket checksum numbytes FileNumber filenameT textz SerialWrite GetTickCount/ timeout SerialRead error_count WaitSOH rbytes_Click? filesize rbytes caption interlock" interlo: ErrorsRecent ErrorsTotal errors ProcessError CommandAbort_Click FlagAbort RXAsciiPut filemode FILE RX.FRM This is the code to handle the XMODEM reception Charles McGuinness [76701,11] These define the various control characters used in receiving XMODEM transferss (R) Start of Header, indicates the start of a packeti (R) End of Transmission, indicates the end of filee (S) Acknowledge, indicates we received the block OK (S) Back space, sent after CANs to clear the host I/P buffert (S) Negative ACK, indicates a bad block (S) CANcel, sent when the user aborts to stop transmitter (R) Control Z, the padding character for the last block Returns number of milliseconds since system boot Set when the user clicks on "abort" buttonm Form_Paint This is the main reception routine! Notes for future . we should have a generalized read with timeout routine rather than replicating the same code.. Number of the next packet Temp var for computing checksum Used to loop 1 to 128 Used to hold the data received What file number to write toi Used to get data from SerialRead Used to time out transmitterR How many bytes received so far? Used to prevent re-entrancy How many errors in a row? How many errors since we started How many bytes read from serial port? If this routine is already running, exit... Unable to open " Switch into binary communications Send the other end a NAK character which should elicit the firstf data packet 0 / 0" Update the displayh At this point in the loop, we're waiting for the next packetx to arrive.o If here, we just NAKed a bad packet. Update the error stats, and abort transfer if too bad. We'll permit up to 10 errors in a row before aborting. There's no limit on how many errors in total we'll allow. Send a bunch of CANs to transmitter to tell it we're quiting, Too many errors" Wait for SOH or EOT Update error count... Has the user clicked the abort button? Send 8 CANs (^X) and then 8 backspaces (^H) OK, let's note what time it is, and start waiting...a Have we been waiting for more than 10 seconds?i Send a NAK (hopefully this will get the transmitter going again), and tally our errorst Did we get EOT? Note for future: It is preferable to NAK the first EOT character, force the transmitter to resend it, and make sure we get anothere EOT. This will cut down on false EOT detection.e OK, we got the EOTE Now we have the SOH, let's get the packet # The first character after the SOH is the packet number. Either it's the one we want, in which case we'll continue, or its ther the previous packet, which means the transmitter didn't see our ack, or its completely different, which means either we're totallye out of sync OR we've just seen a "false" SOH. If its the previous packet, we'll ACK it (again) to get things moving Not the packet we want? The previous packet again?? get the packet 255-#. After the packet number is sent, the complemento of the packet number is sent for safety's sake... Not the packet we want? So far, so good. Now, get the 128 bytes of data... Finally, get the check sumc Do the checksums match? Yes, ACK the transmitter ... write the data to the fileo Binary ... update our statistics No, NAK the transmitter ... and update error statistics CommandAbort_Click This routine sets the abort flag, which is checked in the main loop RXAsciiPut This routine writes a buffer to a file, but filters out any control-Zs from the text first.